package obs.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import obs.form.LoginForm;
import obs.service.RegistService;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class RegistAction extends Action {

    @Autowired
    private RegistService registService;

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {

        LoginForm loginForm = (LoginForm) form;
        boolean ok = registService.regist(loginForm.getUsername(), loginForm.getPassword(), loginForm.getBothday());
        String msg = "";
        if (ok) {
            msg = "regist success.";
        } else {
            msg = "regist failed.";
        }
        ActionMessage message = new ActionMessage(msg, false);
        ActionMessages messages = new ActionMessages();
        messages.add("msg", message);
        saveMessages(request, messages);
        return mapping.findForward("success");
    }

}
